package software.hust.com.hust_last.presenter;

import software.hust.com.hust_last.network.LoginData;
import software.hust.com.hust_last.network.LoginResponse;
import software.hust.com.hust_last.MyApplication;
import software.hust.com.hust_last.callback.ObjectCallback;
import software.hust.com.hust_last.util.AppConfig;
import software.hust.com.hust_last.util.LogUtil;
import software.hust.com.hust_last.util.ToastUtil;
import software.hust.com.hust_last.view.LoginActivity;

/**
 * Created by zwjian on 2017/11/14.
 */

public class LoginPresenter extends BasePresenter<LoginActivity> {
    public LoginPresenter(LoginActivity view) {
        super(view);
    }

    public void loginTrip(String phone, String pwd, String deviceID) {
        LoginData data = new LoginData();
        data.user_phone = phone;
        data.user_password = pwd;
        data.device_token = deviceID;

        data.request(this, new ObjectCallback<LoginResponse>() {
            @Override
            public void onSuccess(int responseCode, LoginResponse response) {
                if ("100".equals(response.result_code)) {
                    ToastUtil.show(getView(), "登录失败");
                    return;
                }
                if (AppConfig.NET_SUCCESS.equals(response.result_code) && getView() != null) {
                    MyApplication.mApplication.loginUserInfo = response.user;
                    MyApplication.mApplication.loginUserInfo.user_token = response.user_token;
                    getView().loginSuccess();
                }
            }

            @Override
            public void onFailure(String errMsg) {
                LogUtil.i("LoginActivity", "errMsg: " + errMsg);
            }
        });
    }
}
